//
//  SlideBigTableView.m
//  艾听歌
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SlideView.h"

@implementation SlideView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.smallTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 44) style:UITableViewStylePlain];

        self.smallTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
        self.smallTableView.delegate = self;
        self.smallTableView.dataSource = self;
        [self addSubview:self.smallTableView];
        [self.smallTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        self.smallTableView.scrollsToTop = YES;
        
        // 注册滑动到顶部的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:wtGoTopNotificationName object:nil];
        
        // 注册离开顶部的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:wtLeaveTopNotificationName object:nil];
    }
    return self;
}

// 接收通知
- (void)notificationAction:(NSNotification *)notification
{
    NSString *notifitionName = notification.name;
    // 如果是滑动到顶部通知
    if ([notifitionName isEqualToString:wtGoTopNotificationName]) {
        NSDictionary *userInfo = notification.userInfo;
        NSString *canScroll = userInfo[@"canScroll"];
        if ([canScroll isEqualToString:@"1"]) {
            self.canScroll = YES;
            self.smallTableView.showsVerticalScrollIndicator = YES;
        }
    } else if ([notifitionName isEqualToString:wtLeaveTopNotificationName]){
        self.smallTableView.contentOffset = CGPointZero;
        self.canScroll = NO;
        self.smallTableView.showsVerticalScrollIndicator = NO;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.canScroll) {
        //如果不能动则半尺(0,0)的偏移量
        [scrollView setContentOffset:CGPointZero];
    }
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY <= 0) {
        // 发出离开通知
        [[NSNotificationCenter defaultCenter] postNotificationName:wtLeaveTopNotificationName object:nil userInfo:@{@"canScroll":@"1"}];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 不能写死数据
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    
    return cell;
}
















@end
